package cn.huijielong.modules.pub.controller;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;

import cn.huijielong.common.utils.PageUtils;
import cn.huijielong.common.utils.Query;
import cn.huijielong.common.utils.RD;
import cn.huijielong.modules.pub.entity.UserOpenidEntity;
import cn.huijielong.modules.pub.service.UserOpenidService;
import cn.huijielong.modules.sys.controller.AbstractController;




/**
 * 用户Id-openid关联表
 * 
 * @author xys
 * @email xys@gmail.com
 * @date 2019-07-03 22:46:44
 */
@RestController
@RequestMapping("/pub/useropenid")
public class UserOpenidController  extends AbstractController{
	@Autowired
	private UserOpenidService userOpenidService;
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{userId}")
	@RequiresPermissions("group:useropenid:info")
	public RD info(@PathVariable("userId") Long userId){
		UserOpenidEntity userOpenid = userOpenidService.queryObject(userId);
		
		return RD.ok().put("userOpenid", userOpenid);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/update")
	public RD save(){
		Query q=Query.createRequest();
		UserOpenidEntity uoe=new UserOpenidEntity();
		uoe.setUserId(getUserId());
		if(q.containsKey("avatarUrl"))
			uoe.setHeadImg(q.getString("avatarUrl"));
		if(q.containsKey("nickName"))
			uoe.setNickName(q.getString("nickName"));
		if(q.containsKey("fromGroup"))
			uoe.setFromGroup(q.getString("fromGroup"));
		if(q.containsKey("remark"))
			uoe.setRemark(q.getString("remark"));
		if(q.containsKey("openid"))
			uoe.setOpenid(q.getString("openid"));
		userOpenidService.update(uoe);
		return RD.ok();
	}
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public RD list(){
		Query q=Query.createRequest();
//		q.addParam("merchantId", this.getMerchant().getMerchantId());
		Page<UserOpenidEntity> page=userOpenidService.queryList(q);
		PageUtils pageUtil = new PageUtils(page, page.getTotal(), q.getLimit(), q.getPage());
		return RD.ok().put("page", pageUtil);
	}
	
}
